package com.sinosoft.dsp.component.bpm.service.core;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.model.SelectItem;

import com.sinosoft.dsp.system.core.i18n.BaseMessages;

@ManagedBean(name = "bpmSelectItemsService")
@ApplicationScoped
public class BpmSelectItemsService implements Serializable {

	private static final long serialVersionUID = 1721122697383851952L;

	private static final String PKG = "com.sinosoft.dsp.component.bpm";

	public static final String SELECT_ITEM_CONTINUE = "continue";

	public static final String SELECT_ITEM_CONTINUE_DEFAULT = "sync";

	public static final String SELECT_ITEM_ENDS = "ends";

	public static final String SELECT_ITEM_ENDS_DEFAULT = "process-instance";

	public static final String SELECT_ITEM_END_STATE_CANCEL = "cancel";

	public static final String SELECT_ITEM_END_STATE_ERROR = "error";

	public static final String SELECT_ITEM_CASCADE_DEFAULT = "0";

	public static final String SELECT_ITEM_ASSIGN = "assign";

	public static final String SELECT_ITEM_ASSIGN_DEFAULT = "competition";

	public static final String SELECT_ITEM_COMPLETE_STRATEGY = "completeStrategy";

	public static final String SELECT_ITEM_COMPLETE_STRATEGY_DEFAULT = "all";

	public static final String SELECT_ITEM_AWOKE = "awokeType";

	public static final String SELECT_ITEM_AWOKE_DEFAULT = "arrivalTo";

	public static final String SELECT_ITEM_AFFIXTYPE = "affixOperationType";

	public static final String SELECT_ITEM_AFFIXTYPE_DEFAULT = "none";

	public static final String SELECT_ITEM_MESSAGEBOARD = "messageBoard";

	public static final String SELECT_ITEM_MESSAGEBOARD_DEFAULT = "none";

	public static final String SELECT_ITEM_LANG = "lang";

	public static final String SELECT_ITEM_LANG_DEFAULT = "juel";

	public static final String SELECT_ITEM_LOCKMODE = "lockmode";

	public static final String SELECT_ITEM_LOCKMODE_DEFAULT = "upgrade";

	public static final String SELECT_ITEM_FORMTYPE = "formType";

	public static final String SELECT_ITEM_FORMTYPE_DEFAULT = "inherit";

	public static final String SELECT_ITEM_DEFINE_FORMTYPE = "defineFormType";

	public static final String SELECT_ITEM_DEFINE_FORMTYPE_DEFAULT = "system";

	public static final String SELECT_ITEM_EVENTTYPE = "eventType";

	public static final String SELECT_ITEM_EVENTTYPE_DEFAULT = "start";

	public static final String SELECT_ITEM_EVENT_DEFINE_TYPE = "eventDefineType";

	public static final String SELECT_ITEM_EVENT_DEFINE_TYPE_DEFAULT = "eventListener";

	public static final String SELECT_ITEM_EVENT_DEFINE_TYPE_EVENTLISTENER = "eventListener";
	public static final String SELECT_ITEM_EVENT_DEFINE_TYPE_HQL = "hql";
	public static final String SELECT_ITEM_EVENT_DEFINE_TYPE_SQL = "sql";
	public static final String SELECT_ITEM_EVENT_DEFINE_TYPE_JAVA = "java";
	public static final String SELECT_ITEM_EVENT_DEFINE_TYPE_SCRIPT = "script";
	public static final String SELECT_ITEM_EVENT_DEFINE_TYPE_MAIL = "mail";

	public static final String SELECT_ITEM_EVENT_PARAMETER_TYPE = "parameterType";

	public static final String SELECT_ITEM_EVENT_PARAMETER_TYPE_DEFAULT = "string";

	private Map<String, List<SelectItem>> items = new HashMap<String, List<SelectItem>>();

	private Map<String, Map<String, String>> itemsMap = new HashMap<String, Map<String, String>>();

	@PostConstruct
	public void load() {
		items.clear();
		itemsMap.clear();

		// 活动执行方式
		List<SelectItem> continueList = new ArrayList<SelectItem>();
		continueList.add(new SelectItem("async", BaseMessages.getString(PKG,
				"service.bpm.select.continue.async")));
		continueList.add(new SelectItem("sync", BaseMessages.getString(PKG,
				"service.bpm.select.continue.sync")));
		continueList.add(new SelectItem("exclusive", BaseMessages.getString(
				PKG, "service.bpm.select.continue.exclusive")));
		items.put(SELECT_ITEM_CONTINUE, continueList);

		// 结束方式
		List<SelectItem> endsList = new ArrayList<SelectItem>();
		endsList.add(new SelectItem("execution", BaseMessages.getString(PKG,
				"service.bpm.select.ends.execution")));
		endsList.add(new SelectItem("process-instance", BaseMessages.getString(
				PKG, "service.bpm.select.ends.process-instance")));
		items.put(SELECT_ITEM_ENDS, endsList);

		// 分配策略
		List<SelectItem> assignList = new ArrayList<SelectItem>();
		assignList.add(new SelectItem("random", BaseMessages.getString(PKG,
				"service.bpm.select.assign.random")));
		assignList.add(new SelectItem("assigned", BaseMessages.getString(PKG,
				"service.bpm.select.assign.assigned")));
		assignList.add(new SelectItem("competition", BaseMessages.getString(
				PKG, "service.bpm.select.assign.competition")));
		assignList.add(new SelectItem("custom", BaseMessages.getString(PKG,
				"service.bpm.select.assign.custom")));
		assignList.add(new SelectItem("countersign", BaseMessages.getString(
				PKG, "service.bpm.select.assign.countersign")));
		items.put(SELECT_ITEM_ASSIGN, assignList);

		// 完成策略
		List<SelectItem> completeStrategy = new ArrayList<SelectItem>();
		completeStrategy.add(new SelectItem("all", BaseMessages.getString(PKG,
				"service.bpm.select.completeStrategy.all")));
		completeStrategy.add(new SelectItem("everyone", BaseMessages.getString(
				PKG, "service.bpm.select.completeStrategy.everyone")));
		completeStrategy.add(new SelectItem("n", BaseMessages.getString(PKG,
				"service.bpm.select.completeStrategy.n")));
		completeStrategy.add(new SelectItem("percentage", BaseMessages
				.getString(PKG,
						"service.bpm.select.completeStrategy.percentage")));
		items.put(SELECT_ITEM_COMPLETE_STRATEGY, completeStrategy);

		// 提醒时间类型
		List<SelectItem> awokeType = new ArrayList<SelectItem>();
		awokeType.add(new SelectItem("arrivalTo", BaseMessages.getString(PKG,
				"service.bpm.select.awokeType.arrivalTo")));
		awokeType.add(new SelectItem("overtime", BaseMessages.getString(PKG,
				"service.bpm.select.awokeType.overtime")));
		awokeType.add(new SelectItem("interval", BaseMessages.getString(PKG,
				"service.bpm.select.awokeType.interval")));
		items.put(SELECT_ITEM_AWOKE, awokeType);

		Map<String, String> awokeTypeMap = new HashMap<String, String>();
		awokeTypeMap.put("arrivalTo", BaseMessages.getString(PKG,
				"service.bpm.select.awokeType.arrivalTo"));
		awokeTypeMap.put("overtime", BaseMessages.getString(PKG,
				"service.bpm.select.awokeType.overtime"));
		awokeTypeMap.put("interval", BaseMessages.getString(PKG,
				"service.bpm.select.awokeType.interval"));
		itemsMap.put(SELECT_ITEM_AWOKE, awokeTypeMap);

		// 附件操作类型
		List<SelectItem> affixType = new ArrayList<SelectItem>();
		affixType.add(new SelectItem("none", BaseMessages.getString(PKG,
				"service.bpm.select.affixType.none")));
		affixType.add(new SelectItem("add", BaseMessages.getString(PKG,
				"service.bpm.select.affixType.add")));
		affixType.add(new SelectItem("view", BaseMessages.getString(PKG,
				"service.bpm.select.affixType.download")));
		items.put(SELECT_ITEM_AFFIXTYPE, affixType);

		// 留言版类型
		List<SelectItem> messageBoard = new ArrayList<SelectItem>();
		messageBoard.add(new SelectItem("none", BaseMessages.getString(PKG,
				"service.bpm.select.messageBoard.none")));
		messageBoard.add(new SelectItem("add", BaseMessages.getString(PKG,
				"service.bpm.select.messageBoard.add")));
		messageBoard.add(new SelectItem("view", BaseMessages.getString(PKG,
				"service.bpm.select.messageBoard.view")));
		items.put(SELECT_ITEM_MESSAGEBOARD, messageBoard);

		// 表达式语言
		List<SelectItem> lang = new ArrayList<SelectItem>();
		lang.add(new SelectItem("juel", BaseMessages.getString(PKG,
				"service.bpm.select.lang.juel")));
		lang.add(new SelectItem("bsh", BaseMessages.getString(PKG,
				"service.bpm.select.lang.bsh")));
		lang.add(new SelectItem("groovy", BaseMessages.getString(PKG,
				"service.bpm.select.lang.groovy")));
		items.put(SELECT_ITEM_LANG, lang);

		Map<String, String> langMap = new HashMap<String, String>();
		langMap.put("juel", BaseMessages.getString(PKG,
				"service.bpm.select.lang.juel"));
		langMap.put("bsh", BaseMessages.getString(PKG,
				"service.bpm.select.lang.bsh"));
		langMap.put("groovy", BaseMessages.getString(PKG,
				"service.bpm.select.lang.groovy"));
		itemsMap.put(SELECT_ITEM_LANG, langMap);

		// 锁定模式
		List<SelectItem> lockmode = new ArrayList<SelectItem>();
		lockmode.add(new SelectItem("none", BaseMessages.getString(PKG,
				"service.bpm.select.lockmode.none")));
		lockmode.add(new SelectItem("read", BaseMessages.getString(PKG,
				"service.bpm.select.lockmode.read")));
		lockmode.add(new SelectItem("upgrade", BaseMessages.getString(PKG,
				"service.bpm.select.lockmode.upgrade")));
		lockmode.add(new SelectItem("upgrade_nowait", BaseMessages.getString(
				PKG, "service.bpm.select.lockmode.upgrade_nowait")));
		lockmode.add(new SelectItem("write", BaseMessages.getString(PKG,
				"service.bpm.select.lockmode.write")));
		items.put(SELECT_ITEM_LOCKMODE, lockmode);

		// 表单类别
		List<SelectItem> formType = new ArrayList<SelectItem>();
		formType.add(new SelectItem("inherit", BaseMessages.getString(PKG,
				"service.bpm.select.formType.inherit")));
		formType.add(new SelectItem("system", BaseMessages.getString(PKG,
				"service.bpm.select.formType.system")));
		formType.add(new SelectItem("custom", BaseMessages.getString(PKG,
				"service.bpm.select.formType.custom")));
		items.put(SELECT_ITEM_FORMTYPE, formType);

		// 流程定义的表单类型
		List<SelectItem> defineFormType = new ArrayList<SelectItem>();
		defineFormType.add(new SelectItem("system", BaseMessages.getString(PKG,
				"service.bpm.select.formType.system")));
		defineFormType.add(new SelectItem("custom", BaseMessages.getString(PKG,
				"service.bpm.select.formType.custom")));
		items.put(SELECT_ITEM_DEFINE_FORMTYPE, defineFormType);

		// 事件触发类别
		List<SelectItem> eventType = new ArrayList<SelectItem>();
		eventType.add(new SelectItem("start", BaseMessages.getString(PKG,
				"service.bpm.select.eventType.start")));
		eventType.add(new SelectItem("end", BaseMessages.getString(PKG,
				"service.bpm.select.eventType.end")));
		items.put(SELECT_ITEM_EVENTTYPE, eventType);

		// 事件类别
		List<SelectItem> eventDefineType = new ArrayList<SelectItem>();
		eventDefineType.add(new SelectItem(
				SELECT_ITEM_EVENT_DEFINE_TYPE_EVENTLISTENER,
				BaseMessages.getString(PKG,
						"service.bpm.select.eventDefineType.eventListener")));
		eventDefineType.add(new SelectItem(SELECT_ITEM_EVENT_DEFINE_TYPE_HQL,
				BaseMessages.getString(PKG,
						"service.bpm.select.eventDefineType.hql")));
		eventDefineType.add(new SelectItem(SELECT_ITEM_EVENT_DEFINE_TYPE_SQL,
				BaseMessages.getString(PKG,
						"service.bpm.select.eventDefineType.sql")));
		eventDefineType.add(new SelectItem(SELECT_ITEM_EVENT_DEFINE_TYPE_JAVA,
				BaseMessages.getString(PKG,
						"service.bpm.select.eventDefineType.java")));
		eventDefineType.add(new SelectItem(
				SELECT_ITEM_EVENT_DEFINE_TYPE_SCRIPT, BaseMessages.getString(
						PKG, "service.bpm.select.eventDefineType.script")));
		eventDefineType.add(new SelectItem(SELECT_ITEM_EVENT_DEFINE_TYPE_MAIL,
				BaseMessages.getString(PKG,
						"service.bpm.select.eventDefineType.mail")));
		items.put(SELECT_ITEM_EVENT_DEFINE_TYPE, eventDefineType);

		// 参数类型
		List<SelectItem> parameterType = new ArrayList<SelectItem>();
		parameterType.add(new SelectItem("string", BaseMessages.getString(PKG,
				"service.bpm.select.parameterType.string")));
		parameterType.add(new SelectItem("byte", BaseMessages.getString(PKG,
				"service.bpm.select.parameterType.byte")));
		parameterType.add(new SelectItem("char", BaseMessages.getString(PKG,
				"service.bpm.select.parameterType.char")));
		parameterType.add(new SelectItem("double", BaseMessages.getString(PKG,
				"service.bpm.select.parameterType.double")));
		parameterType.add(new SelectItem("float", BaseMessages.getString(PKG,
				"service.bpm.select.parameterType.float")));
		parameterType.add(new SelectItem("int", BaseMessages.getString(PKG,
				"service.bpm.select.parameterType.int")));
		parameterType.add(new SelectItem("long", BaseMessages.getString(PKG,
				"service.bpm.select.parameterType.long")));
		parameterType.add(new SelectItem("short", BaseMessages.getString(PKG,
				"service.bpm.select.parameterType.short")));
		parameterType.add(new SelectItem("boolean", BaseMessages.getString(PKG,
				"service.bpm.select.parameterType.boolean")));
		items.put(SELECT_ITEM_EVENT_PARAMETER_TYPE, parameterType);
	}

	public Map<String, List<SelectItem>> getItems() {
		return items;
	}

	public Map<String, Map<String, String>> getItemsMap() {
		return itemsMap;
	}

	@PreDestroy
	public void destroy() {
		items.clear();
		itemsMap.clear();
	}

}
